// pages/goods/comments/index.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        focus: false,
        subjectId: null, //商品id
        content: '', //评价内容
        list: [],
        parentId: 0, //新增一级  0    //如果是第二级评论值为评论的comtId
    },
    //reCommentOne
    // 点击第一级的回复
    reCommentOne(e) {
        let item = e.currentTarget.dataset.item
        console.log("item", item);

        this.setData({
            focus: true,
            parentId: item.comtId
        })
    },
    // 点击第二级的回复
    reCommentTwo(e) {
        let item = e.currentTarget.dataset.item

        this.setData({
            focus: true,
            parentId: item.comtId
        })
    },
    changeContent(e) {
        this.setData({
            content: e.detail
        })
    },
    getList() {
        let obj = {
            url: "/system/comments/list",
            method: "get",
            data: {
                subjectId: this.data.subjectId,
                parentId: 0
            }
        }
        app.request.wxRequest(obj).then(res => {
            if (res.data.code == 200) {
                let list = res.data.data
                this.setData({
                    list
                })
            }
        })
    },

    send() {
        let parentId = this.data.parentId
        let subjectId = this.data.subjectId
        let content = this.data.content

        if (!content) {
            wx.showToast({
                icon: "error",
                title: '请填写评论内容！',
                duration: 500
            })
        } else if (parentId == 0) {
            let obj = {
                url: "/system/comments",
                method: "post",
                data: {
                    parentId: parentId,
                    subjectId: subjectId,
                    content: content
                }
            }
            app.request.wxRequest(obj).then(res => {
                if (res.data.code == 200) {
                    this.setData({
                        content: '',
                        focus: false,
                    })
                    this.getList()
                }
            })
        } else {
            let obj = {
                url: "/system/comments",
                method: "post",
                data: {
                    parentId: parentId,
                    subjectId: subjectId,
                    content: content
                }
            }
            app.request.wxRequest(obj).then(res => {
                if (res.data.code == 200) {
                    this.setData({
                        content: '',
                        focus: false,
                    })
                    this.getList()
                }
            })
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this
        let subjectId = options.id
        that.setData({
            subjectId
        })
        that.getList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})